<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>cyja.js</title>
    <style>
        .login_box { display: none }
    </style>
</head>
<body>
<div class="login_box login_phone">
    <h2>手机号登陆</h2>
    <input type="text" class="input_account" placeholder="请输入账号"><br/>
    <input type="text" class="input_pwd" placeholder="请输入验证码" style="width:80px">
    <input type="button" value="发送验证码" class="btn_code"><br/>
    <input type="button" value="登录" class="btn_login_phone">
</div>
<div class="login_box login_platform">
    <h2>平台登陆弹窗</h2>
    <input type="button" value="登录弹窗" class="btn_login">
</div>
<div class="login_box login_xjzj">
    <h2>指定星际战甲登陆弹窗</h2>
    <input type="button" value="登录弹窗" class="btn_login_xjzj">
</div>
<div class="login_success" hidden>
    <h2>登陆成功</h2>
    <a href="javascript:;" class="btn_logout">退出</a>
</div>
<div class="http">
    <h2>接口请求</h2>
    <input type="button" value="正常请求接口" class="btn_client">
    <input type="button" value="登录后才能领取奖品" class="btn_gift">
</div>
</body>
<script src="/act/all/cdn/jquery/3.4.1/jquery.min.js"></script>
<script src="/act/all/cdn/wechat/jweixin-1.4.0.js"></script>
<script src="/act/all/cdn/cyja/1.0/cyja.js"></script>
<script>
    var platform = 'wechat';
    cyja.config({
        app: "tl3d",
        activity: "appointment",
        version: "20190701",
        server: "tpl0612",
        platform: platform,

        isUserInit: false,
        isWxInit: false,
        isWxLogin: true,
        wxShare: {
            title: '你的好友喊你进驻新服拿好礼！',
            desc: '《新天龙八部》七夕专属新服浪漫开启，点击链接，参与VIP4领奖、转盘抽奖等新服专属活动，多重好礼只属于你！',
            link: location.href,
            imgUrl: 'https://i0.cy.com/xtl/main/20190626/icon_share.jpg'
        },
        ready: function(data) {
            /**
             * data 为 cyja 全局对象 glob
             * 通过 data.code 值判断是否登录成功
             *  -- 0 初始化中 1 初始化成功/登录后 2 活动未开始 3 活动已结束 4 未登录
             **/
            console.log(data);
            data.code === 1 ? initAfter() : initBefore();
        }
    });
    function initBefore() {
        if(platform === 'phone') {
            $('.login_phone').show();
        }else {
            $('.login_platform').show();
            $('.login_xjzj').show();
        }
        $('.login_success').hide();
        var $inputAccount = $('.input_account')
            , $inputPwd = $('.input_pwd');
        $('.btn_code').click(function() {
            cyja.user.getCode($inputAccount.val());
        });
        $('.btn_login_phone').click(function () {
            cyja.user.loginPhone($inputAccount.val(), $inputPwd.val())
        });
        $('.btn_login').click(function() {
            cyja.user.login();      // 默认使用初始化平台登陆
        });
        $('.btn_login_xjzj').click(function() {
            cyja.user.login('warframe');
        })
    }
    function initAfter() {
        $('.login_box').hide();
        $('.login_success').show();
        $('.btn_logout').click(function() {
            cyja.user.logout();
        })
    }

    // 执行没有是否登录限制
    $('.btn_client').click(function() {
        cyja.client.get('/test1', {}, function(res) {
            console.log(res)
        })
    });
    // 执行统一添加判断， 登录后才能执行
    function $click(data) {
        switch (cyja.glob.code) {
            case 0:     // 初始化中...
                break;
            case 1:     // 初始化成功
                data.data && data.data();
                break;
            case 2:     // 活动未开始
                alert('活动未开始');
                break;
            case 3:     // 活动已结束
                alert('活动已结束');
                break;
            case 4:     // 用户未登录
                cyja.user.login();
                break;
            case 99:    // 其他
                break;
            default:
                console.log('未知');
        }
    }
    $('.btn_gift').click(function() {
        cyja.client.get('/test1', {}, function(res) {
            console.log(res)
        })
    }, $click)
</script>
<!--#include virtual="/all/dma/dma_activity.html"-->
</html>